﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZedGraph;

namespace VisionCore
{
    public class LaserInterferometer
    {
        /// <summary>
        /// 偏差(正向)
        /// </summary>
        public List<double> Deviations_Forward { get; set; } = new List<double>();
        /// <summary>
        /// 偏差(反向)
        /// </summary>
        public List<double> Deviations_Reverse { get; set; } = new List<double>();
        /// <summary>
        /// 目标值
        /// </summary>
        public List<double> Targets { get; set; } = new List<double>();
        /// <summary>
        /// 目标值(正向补偿后)
        /// </summary>
        public List<double> Targets_Forward { get; set; } = new List<double>();
        /// <summary>
        /// 目标值(反向补偿后)
        /// </summary>
        public List<double> Targets_Reverse { get; set; } = new List<double>();
        /// <summary>
        /// 数据(正向)
        /// </summary>
        public PointPairList PPDatas_Forward { get; set; } = new PointPairList();
        /// <summary>
        /// 数据(反向)
        /// </summary>
        public PointPairList PPDatas_Reverse { get; set; } = new PointPairList();

        private string filePath;
        public LaserInterferometer(string filePath)
        {
            this.filePath = filePath;
        }

        public void ReadRTL()
        {
            string org = null;

            if (!File.Exists(filePath)) return;

            //读取文件数据
            using (var r = new StreamReader(filePath))
            {
                org = r.ReadToEnd();
            }

            string[] pars = new string[2000];
            //按行解析
            pars = org.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            string targetString;
            int targetNum = 0;
            int targetNum2 = 0;
            bool bStartGet = false;
            bool bStartGet_Tar = false;
            int count = 0;
            int runCount = 1;
            int targetCount = 0;
            List<string> dataList_Deviations = new List<string>();
            List<string> dataList_Targets = new List<string>();

            foreach (var f in pars)
            {
                int ftc = f.IndexOf("Target-count:");
                if (ftc == 0)
                {
                    targetString = f;
                    int idx = f.IndexOf(':');
                    string num = f.Substring(idx + 1);

                    //读取数据的个数
                    targetNum = int.Parse(num);
                }

                if (f.IndexOf("Run-count:") == 0)
                {
                    int idx = f.IndexOf(':');
                    string num = f.Substring(idx + 1);

                    //读取运行的个数
                    runCount = int.Parse(num);
                }

                if (f.IndexOf("Targets :") == 0)
                {
                    bStartGet_Tar = true;
                    continue;
                }

                targetNum2 = targetNum % 5 > 0 ? targetNum / 5 + 1 : targetNum / 5;
                if (bStartGet_Tar && targetCount < targetNum2)
                {
                    dataList_Targets.Add(f);
                    targetCount++;
                }

                if (f.IndexOf("Run Target Data:") == 0)
                {
                    bStartGet = true;
                    continue;
                }

                if (bStartGet && count < targetNum * runCount)
                {
                    dataList_Deviations.Add(f);
                    count++;
                }
            }

            Targets = GetTargets(dataList_Targets);

            Deviations_Forward = GetPosArray(dataList_Deviations, targetNum, 0);

            Targets_Forward = GetTargetArray(Targets, 0);

            PPDatas_Forward = GetTargetPoint(0);


            if (runCount > 1)
            {
                Deviations_Reverse = GetPosArray(dataList_Deviations, targetNum, 1);
                Targets_Reverse = GetTargetArray(Targets, 1);
                PPDatas_Reverse = GetTargetPoint(1);
            }
        }

        /// <summary>
        /// 获取解析数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="count"></param>
        /// <param name="mode">0 正向；1 反向</param>
        /// <returns></returns>
        private List<double> GetPosArray(List<string> list, int count, int mode = 0)
        {
            List<double> posArray = new List<double>();

            if (mode == 0)
            {
                for (int i = 0; i < count; i++)
                {
                    string[] date = list[i].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    double dlbT = double.Parse(date[2]);
                    posArray.Add(dlbT);
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    string[] date = list[count * 2 - i - 1].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    double dlbT = double.Parse(date[2]);
                    posArray.Add(dlbT);
                }
            }

            return posArray;
        }

        /// <summary>
        /// 获取目标值
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<double> GetTargets(List<string> list)
        {
            List<double> targets = new List<double>();

            for (int i = 0; i < list.Count; i++)
            {
                string[] date = list[i].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < date.Length; j++)
                {
                    double dlbT = double.Parse(date[j]);
                    targets.Add(dlbT);
                }
            }

            return targets;
        }

        /// <summary>
        /// 获取目标值(补偿后)
        /// </summary>
        /// <param name="list"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        private List<double> GetTargetArray(List<double> list, int mode = 0)
        {
            List<double> posArray = new List<double>();

            if (mode == 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    posArray.Add(list[i] + Deviations_Forward[i] / 1000);
                }
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    posArray.Add(list[i] + Deviations_Reverse[i] / 1000);
                }
            }

            return posArray;
        }

        /// <summary>
        /// 获取目标值(补前+补后)
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        private PointPairList GetTargetPoint(int mode = 0)
        {
            PointPairList posArray = new PointPairList();

            if (mode == 0)
            {
                for (int i = 0; i < Targets.Count; i++)
                {
                    PointPair mp = new PointPair(Targets[i], Targets_Forward[i]);
                    posArray.Add(mp);
                }
            }
            else
            {
                for (int i = 0; i < Targets.Count; i++)
                {
                    PointPair mp = new PointPair(Targets[i], Targets_Reverse[i]);
                    posArray.Add(mp);
                }
            }

            return posArray;
        }
    }
}
